/*
	$ = juqery;
	clickObj 点击对象
	url 请求地址
	callback 成功返回
	error 错误返回函数
	situation  注册 = 0 ;忘记密码 = 1
*/
(function(){
	var Code = {};

	Code.getcode = function ($,clickObj,url,url2,callback,error,situation,beforeSend){
		Code.isClicked = false;
		$(clickObj).on('click',function(){
		            //点击状态变化

		            var that = this;
		        	var options= {mobile:$('input[name="mobile"]').val(),
					               captcha:$('input[name="captcha"]').val(),
					               type:$(this).attr('value')
            					}
            		if(typeof situation !== 'undefined'){
            			options.situation = situation;
            		}
		           
		            if(Code.isClicked){
		            	return false;
		            }else {
		            	if(!options){
							console.warn('参数不能为空');
							return ;
						}
						for( var i in options){
							if(options[i] === ''){
								layer.msg("手机号或图形验证码不能为空", {icon: 5});
								return ;
							}
						}
						if(!/^1[3578]\d{9}$/.test(options.mobile)){
							layer.alert("手机号格式不正确", {icon: 5,title:false,closeBtn:0});
							return 
						}
						if(!options.captcha) ;

				        $.post(url2,{img_code:options.captcha},function(e){
				                if(e.code!=1){
				                    layer.msg('请输入正确的图形验证码！',{icon:2,time:1000});
				                    return false;
				                }
				                getCode($,url,that,options,callback,error);
				            })
						// console.log(beforeSend());
						// if(!beforeSend()){
						// 	return false;
						// }
		            	
		            }
			})
	}
	
	Code.timeStart = function ($,time,that,isClicked){
					 var style_obj = {
					            "background":'#ddd'
					         };
					var init = [];
					$(that).each(function(i){
						init.push($(that).html());
					})
					var timer = setInterval(function(){
						time--;
					$(that).html("请等待("+time+"s)").css(style_obj);
					if(time < 0){
						$(that).each(function(i){
							$(that).html(init[i]).removeAttr('style');
						})
						Code.isClicked = false;
						clearInterval(timer);
					}else{
						Code.isClicked = true;
						}
				},1000);
			}
		//计时开始
	
	function getCode($,url,that,options,callback,error){
		if( typeof url !== 'string'){
			console.warn('参数url为字符串类型');
			return ;
		}
		
		$.ajaxSetup({
	        headers: {
			    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
			 }
		});

		//获取手机号和验证码方式;
		$.ajax({
			url: url,
			type: 'POST',
			data: options,
			success:callback || '',
			error:error || null,
			
		})
	}
	return window.Code = Code;
})()